package com.example.phoneshop.controller;

import com.example.phoneshop.dao.ProductsDao;
import com.example.phoneshop.entity.Products;
import com.example.phoneshop.service.ProductsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author fang
 * @ data 2018/7/10
 */
@Controller
@RequestMapping(value = "/product")
public class ProductController {

    @Autowired
    private  ProductsService productsService;

    public ProductController(ProductsService productsService){
        this.productsService = productsService;
    }

    @RequestMapping(value = "single", method = RequestMethod.GET)
    public ModelAndView single(){
        ModelAndView model = new ModelAndView("products/single");
        model.addObject("title", "商品详情");
        return model;
    }

    @RequestMapping(value = "all", method = RequestMethod.GET)
    public ModelAndView all(){
        ModelAndView model = new ModelAndView("products/all");
        model.addObject("title", "全部商品");
        return model;
    }

    @RequestMapping(value = "search", method = RequestMethod.GET)
    public ModelAndView search(@RequestParam("search") String search){
        ModelAndView model = null;
        if(search.equals("New")){
           model = new ModelAndView("products/search");
        }else if(search.equals("潮流")){
            model = new ModelAndView("products/more");
        }else{
            model = new ModelAndView("products/other");
        }
        model.addObject("search", search);
        model.addObject("title", "搜索结果");
        return model;
    }

    @RequestMapping("/product")
    public ModelAndView index(){
        ModelAndView model = new ModelAndView("products/product1");
        model.addObject("name","name");
        return model;
    }

    @RequestMapping(value ="/getProductsByName",method = RequestMethod.GET)
    public @ResponseBody Products getProductsInfo(String name){
        Products products = productsService.selectProductByName(name);
        return products;
    }

    @RequestMapping(value = "/allproducts")
    public ModelAndView list(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        ModelAndView model = new ModelAndView("products/list");

        List<Map> products = productsService.findProducts();
        PageInfo pageInfo = new PageInfo<Map>(products,5);
        model.addObject("pageInfo",pageInfo);
        //获得当前页
        model.addObject("pageNum",pageInfo.getPageNum());
        //获得一页显示的条数
        model.addObject("pageSize",pageInfo.getPageSize());
        //是否是第一页
        model.addObject("isFirstPage",pageInfo.isIsFirstPage());
        //获得总页数
        model.addObject("totalPage",pageInfo.getPages());
        //是否是最后一页
        model.addObject("isLastPage",pageInfo.isIsLastPage());
//        List<Map> list = productsService.findProducts();
        model.addObject("list", products);
        return model;
    }

    @RequestMapping(value = "view",method = RequestMethod.GET)
    public ModelAndView find(){
        ModelAndView modelAndView = new ModelAndView("products/products");
        return modelAndView;
    }
}
